//
//  CurrencyViewController.swift
//  CJXSwiftStoryboard
//
//  Created by CJX on 2016/10/22.
//  Copyright © 2016年 caijianxi. All rights reserved.
//

import UIKit
import SVProgressHUD
import SwiftyJSON

class CurrencyViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var myTableView = UITableView(frame: CGRect(x: 0, y: 64, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-64), style: .plain)
    var dataArray = NSArray()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.creatNavVIew()
        self.getData()
        self.creatTableView()
        

    }
    
    
}


extension CurrencyViewController{
    
    fileprivate func creatNavVIew(){
        
        self.automaticallyAdjustsScrollViewInsets = false
        self.view.backgroundColor = UIColor.white
        self.navigationController?.navigationBar.barTintColor = UIColor(red: 94/255.0, green: 200/255.0, blue: 252/255.0, alpha: 1)
        self.title = "CURRENCY"

        
        
        let leftBtn = UIButton(type: .custom)
        leftBtn.setImage(UIImage(named: "nav_back"), for: UIControlState())
        leftBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        leftBtn.addTarget(self, action: #selector(leftBtnClick), for: .touchUpInside)
        let item = UIBarButtonItem(customView: leftBtn)
        self.navigationItem.leftBarButtonItem = item
    }
    
    func leftBtnClick(){
        self.dismiss(animated: true) {
            
        }
    }
}

extension CurrencyViewController{
    fileprivate func getData(){
        
        let urlStr = APIBASE + "main_page=currencies"
        let parma = ["zenid":UserSession.shareInstance.cookie!]
        NetworkTool.shareInstance.request(.post, urlString: urlStr, parmas: parma as [String : NSObject]) { (result, error) in
            if result != nil{
                let josn = JSON(result!)
                print(josn)
                
                let curDic = ((result as? NSDictionary)!["data"]as?NSDictionary)!["currencies"]as?NSDictionary
                self.dataArray = (curDic?.allKeys as? NSArray)!
                
                self.myTableView.reloadData()
            }else{
                SVProgressHUD.showError(withStatus: "get currency data error:" + String(describing: error))
                print("get currency data error:" + String(describing: error))
            }
        }
        
        
    }
}



extension CurrencyViewController{
    fileprivate func creatTableView(){
        
        myTableView.delegate = self
        myTableView.dataSource = self
        self.view.addSubview(myTableView)
        
    }
    
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        }
        
        cell?.textLabel?.text = self.dataArray[indexPath.row]as?String
        
        return cell!
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let urlStr = APIBASE + "main_page=currencies"
        
        let parma = ["currency":String(describing: self.dataArray[indexPath.row]),"zenid":UserSession.shareInstance.cookie!]
        
        NetworkTool.shareInstance.request(.post, urlString: urlStr, parmas: parma as [String : NSObject]) { (result, error) in
            if result != nil{
//                print(result)
                UserDefaults.standard.set(String(describing: self.dataArray[indexPath.row]), forKey: "CURRENCY")
                UserDefaults.standard.set(String(indexPath.row), forKey: "MONEYINDEX")
                UserDefaults.standard.synchronize()
                SVProgressHUD.showSuccess(withStatus: "Choose success!")
                
                self.dismiss(animated: true) {
                    
                }
                
            }else{
                SVProgressHUD.showError(withStatus: "get currency data error:" + String(describing: error))
                print("get currency data error:" + String(describing: error))
            }
        }
        
        
    }
}














